package com.example.rabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: 扎马斯
 * @date: 2024年08月28日 18:23
 * @describe: 点对点模式
 */
@Configuration
public class RabbitMqDirect {

    /**
     * 创建队列
     *
     * @return Queue
     */
    @Bean
    public Queue createQueue() {
        return QueueBuilder.durable(RabbitMqConf.QUEUE_DIRECT).maxLength(1000).build();
    }

    /**
     * 创建交换机
     *
     * @return DirectExchange
     */
    @Bean
    public DirectExchange createExchange() {
        return ExchangeBuilder.directExchange(RabbitMqConf.EXCHANGE_DIRECT).build();
    }

    /**
     * 绑定交换机和队列
     *
     * @param directQueue
     * @param directExchange
     * @return Binding
     */
    @Bean
    public Binding directingBinding(@Qualifier("createQueue") Queue directQueue,@Qualifier("createExchange") DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue).to(directExchange).with(RabbitMqConf.ROUTING_KEY_DIRECT);
    }
}
